import Vue from 'vue'
import constant from '@/utils/constant'
import session from '@/utils/session'
import mainApi from '@/api/main'

const Login = constant.ConstantRouteName.Login;
let responseChain = {
    async statusHandler(response) {
        const status = response.status;
        const data = response.data;
        const config = response.config;
        console.log(response)
        switch(status) {
            case constant.HttpStatus.BAD_REQUEST:
                Vue.prototype.$MessageN.error(data.msg || "请求参数错误!");
                break;
            case constant.HttpStatus.UNAUTHORIZED:// 认证失败，请重新认证!
                console.info("认证失败",config)
                let url = config.url;
                if(url.includes('/sys/relogin')) {
                    session.removeToken();
                    window.location.reload();
                    return Promise.resolve();
                } else {
                    let refreshToken = session.getRefreshToken();
                    await mainApi.doReLogin({refreshToken:refreshToken}).then(result => {
                        session.setToken(result);
                    });
                    let result = await mainApi.doReRequest(config);
                    return Promise.resolve(result);
                }
            case constant.HttpStatus.FORBIDDEN:
                Vue.prototype.$MessageN.error(data.msg || "没有权限，请联系管理员!");
                break;
            case constant.HttpStatus.NOT_FOUND:
                Vue.prototype.$MessageN.error("页面丢了");
                break;
            case constant.HttpStatus.METHOD_NOT_ALLOWED:
                Vue.prototype.$MessageN.error(data.msg || "请求类型不允许!");
                break;
            case constant.HttpStatus.INTERNAL_SERVER_ERROR:
                // if(constant.HttpStatus.FAIL == data.code) {
                //     if(data.msg) {
                //         Vue.prototype.$MessageN.error(data.msg);
                //     } else {
                //         Vue.prototype.$NotificationN.open('Network Error','<strong class="my-notify">请检查 API 是否异常</strong>');
                //     }
                //     break;
                // }
                Vue.prototype.$MessageN.error(data.msg);
                // Vue.prototype.$NotificationN.open('Network Error','<strong class="my-notify">请检查 API 是否异常</strong>');
                break;
            case constant.HttpStatus.SERVICE_UNAVAILABLE:
                console.log("22299999999999999")
                Vue.prototype.$NotificationN.open('Network Error','<strong class="my-notify">请检查 API 是否异常</strong>');
                break;
            default:
                console.log("22222222222===============")
                Vue.prototype.$MessageN.error(data.msg);
                break
        }
        return Promise.reject()
    }
}

export default responseChain;

